package luongvd.appdownload;

import luongvd.appdownload.common.ServerConnectionUtils;
import luongvd.appdownload.common.Utils;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class InstallCheckerService extends Service {

	Context mContext;
	boolean checkInstall;
	String namePackage;
	String userLogin;
	int timeSleepCheckInstall = 30000;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		mContext = this;
		checkInstall = false;
		userLogin = "";
		try {
			Bundle bundle = intent.getExtras();
			namePackage = bundle.getString("namePackage");
		} catch (Exception e) {
			// TODO: handle exception
		}

		//doCheckLogin();
		doCheckAppInstall();
		return super.onStartCommand(intent, flags, startId);
	}

//	void doCheckLogin() {
//		do {
//			try {
//				Thread.sleep(timeSleepCheckLogin);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			userLogin = Utils.getAccountLoginGoogleFirst(mContext);
//			Log.d("Luong", "========================== Check userLogin: "
//					+ userLogin);
//			if (userLogin.length() > 0) {
//				ServerDelegate.connectServerToCheckLogin(handler,
//						ServerDelegate.LINK_CHECK_LOGIN, "1");
//				doCheckAppInstall();
//			} else {
//				Log.d("Luong", "========================== Kill App");
//				Utils.killGooglePlay(mContext);
//
//			}
//		} while (userLogin.length() == 0);
//	}

	void doCheckAppInstall() {
		do {
			checkInstall = Utils.appInstalledOrNot(namePackage, mContext);
			Log.d("Luong", "========================== Check checkInstall: "
					+ checkInstall);
			if (checkInstall != true) {
				try {
					Thread.sleep(timeSleepCheckInstall);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				ServerDelegate.connectServerToCheckInstall(handler,
						ServerDelegate.LINK_SET_DONE_DOWNLOAD, userLogin,
						namePackage);
			}
		} while (checkInstall == false);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.arg1 == ServerConnectionUtils.CONNECT_TO_SERVER_MESSAGE_SUCCESSFUL) {
				try {
					JSONObject dataJSONObject = new JSONObject();
					dataJSONObject = (JSONObject) msg.obj;

					String keyAction = dataJSONObject.getString("action");
					String keyStatus = dataJSONObject.getString("status");
					if (keyAction.equals("setDoneDownload")) {
						if (Integer.parseInt(keyStatus) > 0) {
							Log.d("Luong",
									"==================================== Notify installed successful");
							mContext.stopService(new Intent(mContext,
									InstallCheckerService.class));
						}
					} else if (keyAction.equals("checkHasLogin")) {
						if (Integer.parseInt(keyStatus) > 0) {
							Log.d("Luong",
									"==================================== Notify login status: "
											+ userLogin);

						}
					}
				} catch (Exception e) {
					Toast.makeText(mContext, "Error when connect to server",
							Toast.LENGTH_LONG).show();
				}
			}
		}
	};
}
